lib.use(['element', 'table', 'form','laydate','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var laydate=layui.laydate;
    var deliverHeadTable;
    var searchFrm = layui.Form('searchFrm');
    var probId=common.util.getParam('probId');
    table.set(common.layui.table.config());
    // 渲染表格
    deliverHeadTable = table.render({
        elem: '#deliverHeadTable'
        , url: common.util.createUrl('/sale/deliverHead/list')
        // 对分页请求的参数：page、limit重新设定名称
        ,where:{probId:probId}
        ,toolbar: '#toolbar'
        , height: '320px'
        , cols: [[
             {type:'checkbox'}
            ,{type: 'numbers', title: '编号' }
            ,{ field: 'id',  hide: true }
            ,{  field: 'entId',  hide: true}
            ,{  field: 'id',  hide: true}
            ,{  title: '单据编码', field: 'deliverNo' }
            ,{  title: '单据日期', field: 'deliverDate' }
            ,{  field: 'probId',  hide: true}
            ,{  field: 'probNo',hide: true }
            ,{  field: 'orderId',  hide: true}
            ,{  title: '销售订编码', field: 'orderNo' }
            ,{  field: 'storeId',  hide: true}
            ,{  title: '门店名称', field: 'storeName' }
            ,{  field: 'warehouseId',  hide: true}
            ,{  field: 'warehouseCode' ,  hide: true}
            ,{  title: '仓库名称', field: 'warehouseName' }
            ,{  field: 'customerId',  hide: true}
            ,{  field: 'customerCode' ,  hide: true }
            ,{  title: '客户名称', field: 'customerName' }
            ,{  title: '客户联系人', field: 'customerContactName' }
            ,{  title: '客户电话', field: 'customerContactTel' }
            ,{  title: '送货地址', field: 'customerContactAdd' }
            ,{  title: '单据类型', field: 'deliverType' ,templet: function (row) { return common.dist.getValue("sale.DeliverHead_DeliverTypeEnum",row.deliverType);}}
            ,{  title: '订单状态', field: 'deliverState' ,templet: function (row) { return common.dist.getValue("sale.DeliverHead_DeliverStateEnum",row.deliverState);}}
            ,{  field: 'checkUserId',  hide: true}
            ,{  title: '审核人', field: 'checkUserName' }
            ,{  title: '审核时间', field: 'checkDate' }
            ,{  title: '安装日期', field: 'installDate' }
            ,{  field: 'remark',  hide: true}
            ,{  field: 'createUserId',  hide: true}
            ,{  field: 'createUserName',  hide: true}
            ,{  field: 'createTime',  hide: true}
            ,{  field: 'updateUserId',  hide: true}
            ,{  field: 'updateUserName' }
            ,{ title: '修改时间', field: 'updateTime' }
        ]]
    });

    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        params=common.from.serializeObject('#searchFrm');
        deliverHeadTable=table.reload('deliverHeadTable',{
                where:params,
                page: {curr: 1 }
            }
        )
    }

    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(deliverHeadTableFilter)', function(obj){
        update(obj)
    });

    //头工具栏事件
    table.on('toolbar(deliverHeadTableFilter)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        if(obj.event=='add') {
            var config = {
                busStyle:1,
                title:'新增销售出库单',
                content: ['sale/deliverHead/deliverHeadForm.html?probId='+probId], //iframe的url，no代表不显示滚动条
            }
            parent.common.util.openWindow(busCommon.windowConfig(config))

        }else if(obj.event=="export"){
            var params=common.from.serializeObject('#searchFrm');
            params['pages']=deliverHeadTable.config.page.pages;
            params['limit']=deliverHeadTable.config.page.limit;
            params['probId']=probId;
            parent.parent.common.util.exportWindows('/sale/deliverHead/export',params);
        }else if(obj.event=='del'){
            del(table.checkStatus('deliverHeadTable'))
        }else if(obj.event=='check'){
            check(table.checkStatus('deliverHeadTable'))
        }else if(obj.event=='refresh'){
            searchTable();
        }
    })
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if (!common.auth.hasPermission("sale:deliverHead:update")) {
            return;
        }
        var postData = [];
        if (obj.data.constructor === Array) {
            postData = obj.data.map(function (item) {
                return item['id'];
            });
        } else {
            postData = [obj.data.id]
        }
        if (postData.length != 1) return;
        var url= 'sale/deliverHead/deliverHeadForm.html?id=' + postData[0] + "&probId=" + probId;
        var config = {
            busStyle:1,
            title:'修改销售出库单',
            content: url, //iframe的url，no代表不显示滚动条
        }
        parent.common.util.openWindow(busCommon.windowConfig(config))
    }


    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("sale:deliverHead:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){parent.parent.common.util.error('请选择一条记录进行操作');return;}
        if(obj.data[0].deliverState!=1){parent.parent.common.util.error('[{0}] 当前销售发货单已审核'.format(obj.data[0].deliverNo));return;}
        parent.parent.common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            parent.parent.common.ajax.get('/sale/deliverHead/check/'+postData[0].id, null, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
                parent.parent.common.util.success("审核成功")
            },function(resp){
                top.layer.close(index);
                top.common.util.error(resp.msg)
            })

        });
    }

    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("sale:deliverHead:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){top.common.util.error('请选择一条记录');return;}
        if(obj.data[0].deliverState==2){top.common.util.error('当前发货单已审核,,禁止删除');return;}
        top.layer.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.get('/sale/deliverHead/delete/'+postData[0], {}, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
            },function(resp){
                top.layer.close(index);
                top.common.util.error(resp.msg)
            })

        });
    }
    window.formCallBack=function () {
        searchTable();
    }
});
